import Vue from 'vue';
import iView from 'iview';
import wcSwiper from 'wc-swiper';
import 'wc-swiper/style.css';
import * as Utils from '@/lib/utils';
import * as Api from '@/api';
import {isLogin} from '@/auth/authService';
import '@/assets/reset.css';
import 'iview/dist/styles/iview.css';
import App from './App';
import router from './router';
/* eslint-disable */
Vue.config.productionTip = false;
Vue.prototype.$Utils = Utils;
Vue.prototype.$Api = Api;

// 模拟用户信息
Vue.prototype.CONF = {
    testuser: {
        nickname: 'haha'
    },
    tags: {}
};

Vue.use(wcSwiper);
Vue.use(iView);

console.log('Vue组建加载');

Api.getTagList().then((res) => {
    if (res.ok && res.body && res.body.data.length > 0) {
        let tags = {};
        res.body.data.map((item) => {
            tags[item._id] = item.name;
        })
        console.log(tags);
        Vue.prototype.CONF.tags = tags;
    }
}).catch((error) => {
    console.log(error);
});


if (isLogin()) {
    console.log('已登入');
    Utils.login.getUserInfo().then((loginInfo) => {
        if (loginInfo) {
            console.log(loginInfo);
            Vue.prototype.CONF.user = loginInfo.user;
        }
    });
} else {
    // Utils.login.localLogin({
    //     phone: '15764323489',
    //     password: 'xiangdushen001'
    // }).then(() => {
    //     console.log('token获取成功');
    //     return Utils.login.getUserInfo()
    // }).then((userInfo) => {
    //     if (userInfo.error_msg) {
    //         console.log(userInfo.error_msg);
    //     } else {
    //         Vue.prototype.CONF.user = userInfo.user;
    //     }
    // }).catch((error) => {
    //     console.log(error);
    // });
}

// Utils.login.getUserInfo();
console.log('Vue数据初始化完成');


/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    components: {App},
    template: '<App/>'
});
